import { getI18n, useTranslation } from "react-i18next";
import { AutoComplete, Form, Input, Select } from "antd";
import { createSchemaFieldRule } from "antd-zod";
import { z } from "zod";

import Show from "@/components/Show";
import { validDomainName, validPortNumber } from "@/utils/validators";

import { useFormNestedFieldsContext } from "./_context";

const SERVICE_TYPE_CLOUDRESOURCE = "cloudresource" as const;
const SERVICE_TYPE_CNAME = "cname" as const;

const BizDeployNodeConfigFieldsProviderAliyunWAF = () => {
  const { i18n, t } = useTranslation();

  const { parentNamePath } = useFormNestedFieldsContext();
  const formSchema = z.object({
    [parentNamePath]: getSchema({ i18n }),
  });
  const formRule = createSchemaFieldRule(formSchema);
  const formInst = Form.useFormInstance();
  const initialValues = getInitialValues();

  const fieldServiceType = Form.useWatch([parentNamePath, "serviceType"], formInst);

  return (
    <>
      <Form.Item
        name={[parentNamePath, "region"]}
        initialValue={initialValues.region}
        label={t("workflow_node.deploy.form.aliyun_waf_region.label")}
        rules={[formRule]}
        tooltip={<span dangerouslySetInnerHTML={{ __html: t("workflow_node.deploy.form.aliyun_waf_region.tooltip") }}></span>}
      >
        <Input placeholder={t("workflow_node.deploy.form.aliyun_waf_region.placeholder")} />
      </Form.Item>

      <Form.Item
        name={[parentNamePath, "serviceVersion"]}
        initialValue={initialValues.serviceVersion}
        label={t("workflow_node.deploy.form.aliyun_waf_service_version.label")}
        rules={[formRule]}
      >
        <Select placeholder={t("workflow_node.deploy.form.aliyun_waf_service_version.placeholder")}>
          <Select.Option key="3.0" value="3.0">
            3.0
          </Select.Option>
        </Select>
      </Form.Item>

      <Form.Item
        name={[parentNamePath, "serviceType"]}
        initialValue={initialValues.serviceType}
        label={t("workflow_node.deploy.form.aliyun_waf_service_type.label")}
        rules={[formRule]}
      >
        <Select
          options={[SERVICE_TYPE_CLOUDRESOURCE, SERVICE_TYPE_CNAME].map((s) => ({
            value: s,
            label: t(`workflow_node.deploy.form.aliyun_waf_service_type.option.${s}.label`),
          }))}
          placeholder={t("workflow_node.deploy.form.aliyun_waf_service_type.placeholder")}
        />
      </Form.Item>

      <Form.Item
        name={[parentNamePath, "instanceId"]}
        initialValue={initialValues.instanceId}
        label={t("workflow_node.deploy.form.aliyun_waf_instance_id.label")}
        rules={[formRule]}
        tooltip={<span dangerouslySetInnerHTML={{ __html: t("workflow_node.deploy.form.aliyun_waf_instance_id.tooltip") }}></span>}
      >
        <Input placeholder={t("workflow_node.deploy.form.aliyun_waf_instance_id.placeholder")} />
      </Form.Item>

      <Show when={fieldServiceType === SERVICE_TYPE_CLOUDRESOURCE}>
        <Form.Item
          name={[parentNamePath, "resourceProduct"]}
          initialValue={initialValues.resourceProduct}
          label={t("workflow_node.deploy.form.aliyun_waf_resource_product.label")}
          rules={[formRule]}
        >
          <AutoComplete
            options={["ecs", "clb4", "clb7", "nlb"].map((value) => ({ value }))}
            placeholder={t("workflow_node.deploy.form.aliyun_waf_resource_product.placeholder")}
            filterOption={(inputValue, option) => option!.value.toLowerCase().includes(inputValue.toLowerCase())}
          />
        </Form.Item>

        <Form.Item
          name={[parentNamePath, "resourceId"]}
          initialValue={initialValues.resourceId}
          label={t("workflow_node.deploy.form.aliyun_waf_resource_id.label")}
          rules={[formRule]}
        >
          <Input placeholder={t("workflow_node.deploy.form.aliyun_waf_resource_id.placeholder")} />
        </Form.Item>

        <Form.Item
          name={[parentNamePath, "resourcePort"]}
          initialValue={initialValues.resourcePort}
          label={t("workflow_node.deploy.form.aliyun_waf_resource_port.label")}
          rules={[formRule]}
        >
          <Input type="number" min={1} max={65535} placeholder={t("workflow_node.deploy.form.aliyun_waf_resource_port.placeholder")} />
        </Form.Item>
      </Show>

      <Form.Item
        name={[parentNamePath, "domain"]}
        initialValue={initialValues.domain}
        label={t("workflow_node.deploy.form.aliyun_waf_domain.label")}
        extra={t("workflow_node.deploy.form.aliyun_waf_domain.help")}
        rules={[formRule]}
      >
        <Input allowClear placeholder={t("workflow_node.deploy.form.aliyun_waf_domain.placeholder")} />
      </Form.Item>
    </>
  );
};

const getInitialValues = (): Nullish<z.infer<ReturnType<typeof getSchema>>> => {
  return {
    region: "",
    serviceVersion: "3.0",
    instanceId: "",
    resourceProduct: "",
    resourceId: "",
    resourcePort: 443,
  };
};

const getSchema = ({ i18n = getI18n() }: { i18n?: ReturnType<typeof getI18n> }) => {
  const { t } = i18n;

  return z
    .object({
      region: z.string().nonempty(t("workflow_node.deploy.form.aliyun_waf_region.placeholder")),
      serviceVersion: z.literal("3.0", t("workflow_node.deploy.form.aliyun_waf_service_version.placeholder")),
      serviceType: z.literal([SERVICE_TYPE_CLOUDRESOURCE, SERVICE_TYPE_CNAME], t("workflow_node.deploy.form.aliyun_waf_service_type.placeholder")),
      instanceId: z.string().nonempty(t("workflow_node.deploy.form.aliyun_waf_instance_id.placeholder")),
      resourceProduct: z.string().nullish(),
      resourceId: z.string().nullish(),
      resourcePort: z.preprocess((v) => (v == null || v === "" ? void 0 : Number(v)), z.number().nullish()),
      domain: z
        .string()
        .nullish()
        .refine((v) => {
          return !v || validDomainName(v!, { allowWildcard: true });
        }, t("common.errmsg.domain_invalid")),
    })
    .superRefine((values, ctx) => {
      switch (values.serviceType) {
        case SERVICE_TYPE_CLOUDRESOURCE:
          {
            if (!values.resourceProduct) {
              ctx.addIssue({
                code: "custom",
                message: t("workflow_node.deploy.form.aliyun_waf_resource_product.placeholder"),
                path: ["resourceProduct"],
              });
            }

            if (!values.resourceId) {
              ctx.addIssue({
                code: "custom",
                message: t("workflow_node.deploy.form.aliyun_waf_resource_id.placeholder"),
                path: ["resourceId"],
              });
            }

            if (!validPortNumber(values.resourcePort!)) {
              ctx.addIssue({
                code: "custom",
                message: t("workflow_node.deploy.form.aliyun_waf_resource_port.placeholder"),
                path: ["resourcePort"],
              });
            }
          }
          break;
      }
    });
};

const _default = Object.assign(BizDeployNodeConfigFieldsProviderAliyunWAF, {
  getInitialValues,
  getSchema,
});

export default _default;
